package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：yangjian
 * @date ：Created in 2019/7/5
 * @description ：角色控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("findAll")
    public Result findAll(){

        try{
            List<Role> roles = roleService.findAll();
            return new Result(true, MessageConst.ACTION_SUCCESS,roles);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }


     /*
    查询 权限列表and菜单列表
     */

    @RequestMapping("/getAllPermissionAndMenu")
    public Result getAllPermissionAndMenu(){
        Map<String,Object> map=new HashMap<>();
        try{
            List<Permission> permissionList = roleService.findAllPermission();
            if(permissionList!=null&&permissionList.size()>0){
                map.put("permissionList",permissionList);
            }
            List<Menu> menuList = roleService.findAllMenu();
            if(menuList!=null&&menuList.size()>0){
                map.put("menuList",menuList);
            }
            return new Result(true,MessageConst.ACTION_FAIL,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }

    /*
    新增角色
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] pIds, Integer[] mIds) {
        try {
            roleService.add(role,pIds,mIds);
            return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_ROLE_FALL);
        }
    }

    /*
    根据条件查询 角色 并分页
     */

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = roleService.pageQuery(queryPageBean);
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    /*
    编辑回显角色信息
     */
    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer id){
        try{
            Role role = roleService.findRoleById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }

    /*
    权限列表 菜单列表 回显
     */
    @RequestMapping("/findPermissionAndMenuByRoleId")
    public Result findPermissionAndMenuByRoleId(Integer id){
        try{
            Map map = new HashMap();
            List<Integer> pIds = roleService.findPermissionIdsByRoleId(id);
            if(pIds!=null&&pIds.size()>0){
                map.put("pIds",pIds);
            }
            List<Integer> mIds = roleService.findMenuIdsByRoleId(id);
            if(mIds!=null&&mIds.size()>0){
                map.put("mIds",mIds);
            }
            return new Result(true,MessageConst.ACTION_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }


    /*
   编辑
    */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[]pIds,Integer[]mIds){
        try{
            roleService.edit(role,pIds,mIds);
            return new Result(true,MessageConst.ACTION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }















    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();

        }
        return new Result(false, MessageConst.ACTION_FAIL);
    }

//    @RequestMapping("/edit")
//    public Result edit(Role role,Integer[] pIds,Integer[] mids){
//        try{
//            roleService.edit(role,pIds,mids);
//            return new Result(true,MessageConst.EDIT_ROLE_SUCCESS);
//
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConst.EDIT_ROLE_FALL);
//        }
//    }


}
